﻿using ActivateYourGlutes.Model.Contracts;
using ActivateYourGlutes.Model.Internal;
using AutoMapper;
using Account=ActivateYourGlutes.Model.Internal.Account;
using Activity=ActivateYourGlutes.Model.Internal.Activity;
using Body=ActivateYourGlutes.Model.Internal.Body;
using Goal=ActivateYourGlutes.Model.Internal.Goal;
using Measurement=ActivateYourGlutes.Model.Internal.Measurement;
using MeasurementType=ActivateYourGlutes.Model.Internal.MeasurementType;
using UnitOfLength=ActivateYourGlutes.Model.Internal.UnitOfLength;
using UnitOfWeight=ActivateYourGlutes.Model.Internal.UnitOfWeight;

namespace ActivateYourGlutes.Model
{
    public static class ModelBootstrapper
    {
        public static void Initialise()
        {
            Mapper.CreateMap<Account, Contracts.Account>();
            Mapper.CreateMap<Activity, Contracts.Activity>();
            Mapper.CreateMap<Body, Contracts.Body>().
                ForMember(bp => bp.Measurements, opt => opt.MapFrom(b => b.MeasurementSet));
            Mapper.CreateMap<Goal, Contracts.Goal>();
            Mapper.CreateMap<Measurement, Contracts.Measurement>().
                ForMember(m => m.LocalisedValue, opt => opt.Ignore());
            Mapper.CreateMap<MeasurementType, Contracts.MeasurementType>();
            Mapper.CreateMap<UnitOfLength, Contracts.UnitOfLength>();
            Mapper.CreateMap<UnitOfWeight, Contracts.UnitOfWeight>();
            Mapper.CreateMap<Contracts.MeasurementType, MeasurementType>();
            Mapper.CreateMap<ErrorLog, Error>().ConvertUsing(ConvertError);
        }

        private static Error ConvertError(ErrorLog source)
        {
            Error error = new Error();
            error.Timestamp = source.Date;
            error.Message = source.Message;
            error.Exception = source.Exception;
            error.Id = source.Id;

            if (source.Level == "WARN")
                error.ErrorLevel = ErrorLevelEnum.Warning;
            else if (source.Level == "INFO")
                error.ErrorLevel = ErrorLevelEnum.Info;
            else if (source.Level == "FATAL")
                error.ErrorLevel = ErrorLevelEnum.Fatal;
            else
                error.ErrorLevel = ErrorLevelEnum.Error;
            return error;
        }
    }
}